package com.gdcontrol.desktop.visao.manutencao;

import com.gdcontrol.desktop.controle.manutencao.ControleSincronizacao;
import com.gdcontrol.desktop.visao.VisaoPadrao;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JFileChooser;

/**
 *
 * @author gustavo
 */
public class VisaoSincronizacao extends VisaoPadrao {

    /**
     * Creates new form VisaoManutencaoSincronizacao
     */
    public VisaoSincronizacao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try {
            this.edSeuIp.setText(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldEnvio = new javax.swing.JPanel();
        edArquivos = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btSelecionar = new javax.swing.JButton();
        btEnviar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        edIp = new javax.swing.JTextField();
        fieldRecebimento = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        edSeuIp = new javax.swing.JTextField();
        btSincronizar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sincronização");

        fieldEnvio.setBorder(javax.swing.BorderFactory.createTitledBorder("Envio"));

        jLabel1.setText("Arquivos:");

        btSelecionar.setText("Selecionar");
        btSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarActionPerformed(evt);
            }
        });

        btEnviar.setText("Enviar");
        btEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEnviarActionPerformed(evt);
            }
        });

        jLabel2.setText("IP:");

        edIp.setText("10.60.46.17");

        javax.swing.GroupLayout fieldEnvioLayout = new javax.swing.GroupLayout(fieldEnvio);
        fieldEnvio.setLayout(fieldEnvioLayout);
        fieldEnvioLayout.setHorizontalGroup(
            fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldEnvioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btEnviar)
                    .addGroup(fieldEnvioLayout.createSequentialGroup()
                        .addGroup(fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(fieldEnvioLayout.createSequentialGroup()
                                .addComponent(edArquivos, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btSelecionar))
                            .addComponent(edIp, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(256, Short.MAX_VALUE))
        );
        fieldEnvioLayout.setVerticalGroup(
            fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldEnvioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edArquivos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(btSelecionar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(fieldEnvioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btEnviar)
                .addContainerGap())
        );

        fieldRecebimento.setBorder(javax.swing.BorderFactory.createTitledBorder("Recebimento"));

        jLabel3.setText("Seu IP:");

        edSeuIp.setEnabled(false);

        btSincronizar.setText("Sincronizar");
        btSincronizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSincronizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout fieldRecebimentoLayout = new javax.swing.GroupLayout(fieldRecebimento);
        fieldRecebimento.setLayout(fieldRecebimentoLayout);
        fieldRecebimentoLayout.setHorizontalGroup(
            fieldRecebimentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldRecebimentoLayout.createSequentialGroup()
                .addGroup(fieldRecebimentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fieldRecebimentoLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edSeuIp, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(fieldRecebimentoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btSincronizar)))
                .addContainerGap(472, Short.MAX_VALUE))
        );
        fieldRecebimentoLayout.setVerticalGroup(
            fieldRecebimentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldRecebimentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(fieldRecebimentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(edSeuIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSincronizar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fieldEnvio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(fieldRecebimento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldRecebimento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarActionPerformed
        JFileChooser file = new JFileChooser();
        file.setFileSelectionMode(JFileChooser.FILES_ONLY);
        file.setMultiSelectionEnabled(true);
        int i = file.showOpenDialog(this);
        if (i == 1) {
            edArquivos.setText("");
        } else {
            File[] arquivos = file.getSelectedFiles();
            for (File f : arquivos) {
                edArquivos.setText(edArquivos.getText() + f.getPath() + ";");
            }
        }

    }//GEN-LAST:event_btSelecionarActionPerformed

    private void btEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEnviarActionPerformed
        this.controle.enviaArquivos(this.edIp.getText(), this.edArquivos.getText());
    }//GEN-LAST:event_btEnviarActionPerformed

    private void btSincronizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSincronizarActionPerformed
        this.controle.iniciaServidor();
    }//GEN-LAST:event_btSincronizarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VisaoSincronizacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VisaoSincronizacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VisaoSincronizacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VisaoSincronizacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VisaoSincronizacao dialog = new VisaoSincronizacao(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEnviar;
    private javax.swing.JButton btSelecionar;
    private javax.swing.JButton btSincronizar;
    private javax.swing.JTextField edArquivos;
    private javax.swing.JTextField edIp;
    private javax.swing.JTextField edSeuIp;
    private javax.swing.JPanel fieldEnvio;
    private javax.swing.JPanel fieldRecebimento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

    ControleSincronizacao controle = new ControleSincronizacao();

    @Override
    public ControleSincronizacao getControle() {
        return this.controle;
    }
}
